package com.drdj.exam;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ConnectMysql extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connect_mysql);
		
		int res = getResources().getIdentifier("btnExecuteQuery", "id", getPackageName());
		Button btnExecuteQuery = (Button) findViewById(res);
		
		btnExecuteQuery.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					HttpClient httpClient = new DefaultHttpClient();
					HttpPost httpPost = new HttpPost("http://metal07.cafe24.com/andro/andro_sql_select.php");
					HttpResponse response = httpClient.execute(httpPost);
					HttpEntity entity = response.getEntity();
					InputStream is = entity.getContent();
					
					BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
					StringBuilder sb = new StringBuilder();
					String line = null;
					while((line = br.readLine()) != null ){
						sb.append(line + "\n");
					}
					is.close();
					String result = sb.toString();
					
					JSONArray jArray = new JSONArray(result);
					StringBuilder sbResult = new StringBuilder();
					for(int i = 0; i < jArray.length(); i++){
						JSONObject jObject = jArray.getJSONObject(i);
						if(i == 0){
							Iterator<?> iter = jObject.keys();
							while(iter.hasNext()){
								sbResult.append(iter.next().toString()+"\t");
							}
							sbResult.append("\n");
						}
						sbResult.append(jObject.get("name") + "\t" + jObject.get("age") + "\t" + jObject.get("email") + "\n");
					}
					
					
					TextView tvResult = (TextView)findViewById(R.id.tvQueryResult);
					tvResult.setText(sbResult.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});

		
		Button btnQueryInsert = (Button) findViewById(R.id.btnQueryInsert);
		
		btnQueryInsert.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					String sql = "Update kitty_member set name='qqq1', age=12 where email='eee'";
					
					ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
					arrayList.add(new BasicNameValuePair("sql", sql));
					
					HttpClient httpClient = new DefaultHttpClient();
					HttpPost httpPost = new HttpPost("http://metal07.cafe24.com/andro/andro_sql_query.php");
					httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
					HttpResponse response = httpClient.execute(httpPost);
					HttpEntity entity = response.getEntity();
					InputStream is = entity.getContent();
					
					
					BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
					StringBuilder sb = new StringBuilder();
					String line = null;
					while((line = br.readLine()) != null ){
						sb.append(line + "\n");
					}
					is.close();
					String result = sb.toString();
					
					
					TextView tvResult = (TextView)findViewById(R.id.tvQueryResult);
					tvResult.setText(result);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
	}
	
}
